var needle = require('needle');
var redisManager = require('./redisManager');

var options = {
    headers:{
        'Content-Type':'application/json'
    }
}
exports.buildTemplateMsg = function(touser, url, data, callback){
    callback({
        touser : touser,
        template_id: 'XHSPe829YV_yIC4tjV5QVsnLJcBC9Q4-3kfvjI9BEn4',
        url: url,
        topcolor: '#44B549',
        data: {
            first: {
                value: data.title
            },
            time: {
                value: data.time
            },
            account: {
                value: data.userName,
                color: '#173177'
            },
            type: {
                value: data.type
            },
            creditChange: {
                value: data.creditChange,
                color: '#173177'
            },
            number: {
                value: data.score,
                color: '#173177'
            },
            creditName: {
                value: data.creditName,
                color: '#173177'
            },
            amount: {
                value: data.total,
                color: '#173177'
            },
            remark: '点击查看我的账户余额'
        }
    })
}

exports.sendTemplateMsg = function(msgJson, callback){
    redisManager.getAccesstoken(function(err, accessToken){
        var url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token='+accessToken;
        var postdata = JSON.stringify(msgJson)
        needle.post(url, postdata, options, function cb(err,resp,body){
            callback(err, body);
        });
    })
}

